library(foreign)
library(effects)
library(nnet)
library(MASS)
library(dplyr)
library(tidyverse)
library(plyr) 
library(haven)
library(simcf)
library(ggplot2)
options(scipen=20)


## read analysis_output
tests <- read_delim(file = "tests_output_RAW.txt", delim = "\t", quote = "", col_names = TRUE)

## adjust analysis output so that county1 always has a greater value
## than county2.  This means that county1 is a treatment county.  Note
## that the chi-squared test statistic does not have sides and hence
## flipping does not affect it.
rows2flip <- which(tests$value1 < tests$value2)
for (row2flip in rows2flip) {
  ## flip county names
  temp <- tests$county1[row2flip]     
  tests$county1[row2flip] <- tests$county2[row2flip]    
  tests$county2[row2flip] <- temp
  ## flip values
  temp <- tests$value1[row2flip]     
  tests$value1[row2flip] <- tests$value2[row2flip]    
  tests$value2[row2flip] <- temp
  ## flip proportions
  temp <- tests$proportion1[row2flip]     
  tests$proportion1[row2flip] <- tests$proportion2[row2flip]    
  tests$proportion2[row2flip] <- temp
}

## add variables to test matrix; most important is hours_change_diff,
## which is the difference in some early voting measure.

## set borderankcutoff
borderrankcutoff <- 40

tests <- tests %>% mutate(prop_diff = (proportion1 - proportion2) * 100,
                          "EIP Difference" = (value1 - value2), # names w/ spaces are needed for neat legend titles
                          "Significance" = ifelse(p_value <= 0.05,"p < 0.05","p > 0.05"),
                          race = revalue(race, c("white" = "White", "black" = "Black","latino"="Latino")),
                          party = revalue(party, c("dem" = "Democrat", "rep" = "Republican","ind"="Independent")),
                          group = paste(race, party, sep = ' '))

  
# set colors
notsig_color <- "gray60"
sig_color <- "black"

## For the appendix: plot all tests ##

## Difference in proportions by difference in change in total hours ##

plot2save <- ggplot(filter(tests[order(tests$Significance,decreasing=T),], 
                           border_measure == "totalhoursDID",
                           test_type == "change_distr_novote2016_given_novote2012"), 
                    aes(x = `EIP Difference`, y = prop_diff)) +
  geom_point(aes(color = `Significance`, size=`EIP Difference`),shape = 1) +
  theme_bw()+theme(strip.background = element_rect(fill="white"))+
  scale_color_manual(values = c(sig_color, notsig_color)) +
  scale_y_continuous(breaks = seq(-30, 30, 10)) + 
  facet_grid(party ~ race) +  # removed:  scales = "free_y",
  labs(y = "Percentage point difference in fraction no vote, given no vote in 2012",
       x = "Difference in Total EIP hour changes\n")

ggsave (plot2save, filename = paste("novote_TOTALhours_RAW", ".pdf", sep = ""),
        width = 7, height = 7)

plot2save <- ggplot(filter(tests[order(tests$Significance,decreasing=T),], 
                           border_measure == "totalhoursDID",
                           test_type == "change_distr_vote2016_given_vote2012"), 
                    aes(x = `EIP Difference`, y = prop_diff)) +
  geom_point(aes(color = `Significance`, size=`EIP Difference`),shape = 1) +
  theme_bw()+theme(strip.background = element_rect(fill="white"))+
  scale_color_manual(values = c(sig_color, notsig_color)) +
  scale_y_continuous(breaks = seq(-30, 30, 10)) + 
  facet_grid(party ~ race) +  # removed:  scales = "free_y",
  labs(y = "Percentage point difference in fraction vote, given vote in 2012",
       x = "Difference in Total EIP hour changes\n")

ggsave (plot2save, filename = paste("vote_TOTALhours_RAW", ".pdf", sep = ""),
        width = 7, height = 7)

## Difference in proportions by difference in change in evening hours ##

plot2save <- ggplot(filter(tests[order(tests$Significance,decreasing=T),], 
                           border_measure == "evehoursDID",
                           test_type == "change_distr_novote2016_given_novote2012"), 
                    aes(x = `EIP Difference`, y = prop_diff)) +
  geom_point(aes(color = `Significance`, size=`EIP Difference`),shape = 1) +
  theme_bw()+theme(strip.background = element_rect(fill="white"))+
  scale_color_manual(values = c(sig_color, notsig_color)) +
  scale_y_continuous(breaks = seq(-30, 30, 10)) + 
  facet_grid(party ~ race) +  # removed:  scales = "free_y",
  labs(y = "Percentage point difference in fraction no vote, given no vote in 2012",
       x = "Difference in Evening EIP hour changes\n")

ggsave (plot2save, filename = paste("novote_EVEhours_RAW", ".pdf", sep = ""),
        width = 7, height = 7)

plot2save <- ggplot(filter(tests[order(tests$Significance,decreasing=T),], 
                           border_measure == "evehoursDID",
                           test_type == "change_distr_vote2016_given_vote2012"), 
                    aes(x = `EIP Difference`, y = prop_diff)) +
  geom_point(aes(color = `Significance`, size=`EIP Difference`),shape = 1) +
  theme_bw()+theme(strip.background = element_rect(fill="white"))+
  scale_color_manual(values = c(sig_color, notsig_color)) +
  scale_y_continuous(breaks = seq(-30, 30, 10)) + 
  facet_grid(party ~ race) +  # removed:  scales = "free_y",
  labs(y = "Percentage point difference in fraction vote, given vote in 2012",
       x = "Difference in Evening EIP hour changes\n")

ggsave (plot2save, filename = paste("vote_EVEhours_RAW", ".pdf", sep = ""),
        width = 7, height = 7)

## Difference in proportions by difference in change in saturday hours ##

plot2save <- ggplot(filter(tests[order(tests$Significance,decreasing=T),], 
                           border_measure == "sathoursDID",
                           test_type == "change_distr_novote2016_given_novote2012"), 
                    aes(x = `EIP Difference`, y = prop_diff)) +
  geom_point(aes(color = `Significance`, size=`EIP Difference`),shape = 1) +
  theme_bw()+theme(strip.background = element_rect(fill="white"))+
  scale_color_manual(values = c(sig_color, notsig_color)) +
  scale_y_continuous(breaks = seq(-30, 30, 10)) + 
  facet_grid(party ~ race) +  # removed:  scales = "free_y",
  labs(y = "Percentage point difference in fraction no vote, given no vote in 2012",
       x = "Difference in Saturday EIP hour changes\n")

ggsave (plot2save, filename = paste("novote_SAThours_RAW", ".pdf", sep = ""),
        width = 7, height = 7)

plot2save <- ggplot(filter(tests[order(tests$Significance,decreasing=T),], 
                           border_measure == "sathoursDID",
                           test_type == "change_distr_vote2016_given_vote2012"), 
                    aes(x = `EIP Difference`, y = prop_diff)) +
  geom_point(aes(color = `Significance`, size=`EIP Difference`),shape = 1) +
  theme_bw()+theme(strip.background = element_rect(fill="white"))+
  scale_color_manual(values = c(sig_color, notsig_color)) +
  scale_y_continuous(breaks = seq(-30, 30, 10)) + 
  facet_grid(party ~ race) +  # removed:  scales = "free_y",
  labs(y = "Percentage point difference in fraction vote, given vote in 2012",
       x = "Difference in Saturday EIP hour changes\n")

ggsave (plot2save, filename = paste("vote_SAThours_RAW", ".pdf", sep = ""),
        width = 7, height = 7)

## Difference in proportions by difference in change in sunday hours ##

plot2save <- ggplot(filter(tests[order(tests$Significance,decreasing=T),], 
                           border_measure == "sunhoursDID",
                           test_type == "change_distr_novote2016_given_novote2012"), 
                    aes(x = `EIP Difference`, y = prop_diff)) +
  geom_point(aes(color = `Significance`, size=`EIP Difference`),shape = 1) +
  theme_bw()+theme(strip.background = element_rect(fill="white"))+
  scale_color_manual(values = c(sig_color, notsig_color)) +
  scale_y_continuous(breaks = seq(-30, 30, 10)) + 
  facet_grid(party ~ race) +  # removed:  scales = "free_y",
  labs(y = "Percentage point difference in fraction no vote, given no vote in 2012",
       x = "Difference in Sunday EIP hour changes\n")

ggsave (plot2save, filename = paste("novote_SUNhours_RAW", ".pdf", sep = ""),
        width = 7, height = 7)

plot2save <- ggplot(filter(tests[order(tests$Significance,decreasing=T),], 
                           border_measure == "sunhoursDID",
                           test_type == "change_distr_vote2016_given_vote2012"), 
                    aes(x = `EIP Difference`, y = prop_diff)) +
  geom_point(aes(color = `Significance`, size=`EIP Difference`),shape = 1) +
  theme_bw()+theme(strip.background = element_rect(fill="white"))+
  scale_color_manual(values = c(sig_color, notsig_color)) +
  scale_y_continuous(breaks = seq(-30, 30, 10)) + 
  facet_grid(party ~ race) +  # removed:  scales = "free_y",
  labs(y = "Percentage point difference in fraction vote, given vote in 2012",
       x = "Difference in Sunday EIP hour changes\n")

ggsave (plot2save, filename = paste("vote_SUNhours_RAW", ".pdf", sep = ""),
        width = 7, height = 7)

## Difference in proportions by difference in change in sites hours ##

plot2save <- ggplot(filter(tests[order(tests$Significance,decreasing=T),], 
                           border_measure == "sitesDID",
                           test_type == "change_distr_novote2016_given_novote2012"), 
                    aes(x = `EIP Difference`, y = prop_diff)) +
  geom_point(aes(color = `Significance`, size=`EIP Difference`),shape = 1) +
  theme_bw()+theme(strip.background = element_rect(fill="white"))+
  scale_color_manual(values = c(sig_color, notsig_color)) +
  scale_y_continuous(breaks = seq(-30, 30, 10)) + 
  facet_grid(party ~ race) +  # removed:  scales = "free_y",
  labs(y = "Percentage point difference in fraction no vote, given no vote in 2012",
       x = "Difference in Sunday EIP hour changes\n")

ggsave (plot2save, filename = paste("novote_SITES_RAW", ".pdf", sep = ""),
        width = 7, height = 7)


plot2save <- ggplot(filter(tests[order(tests$Significance,decreasing=T),], 
                           border_measure == "sitesDID",
                           test_type == "change_distr_vote2016_given_vote2012"), 
                    aes(x = `EIP Difference`, y = prop_diff)) +
  geom_point(aes(color = `Significance`, size=`EIP Difference`),shape = 1) +
  theme_bw()+theme(strip.background = element_rect(fill="white"))+
  scale_color_manual(values = c(sig_color, notsig_color)) +
  scale_y_continuous(breaks = seq(-30, 30, 10)) + 
  facet_grid(party ~ race) +  # removed:  scales = "free_y",
  labs(y = "Percentage point difference in fraction vote, given vote in 2012",
       x = "Difference in Sunday EIP hour changes\n")

ggsave (plot2save, filename = paste("vote_SITES_RAW", ".pdf", sep = ""),
        width = 7, height = 7)

stop()


